package com.largeOrange.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.largeOrange.common.R;
import com.largeOrange.pojo.Category;
import com.largeOrange.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 大橙子
 * @version 1.0
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page> getPage(int page,int pageSize){
        Page<Category> categoryPage = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //添加条件
        wrapper.orderByAsc(Category::getSort);
        //执行条件
        categoryService.page(categoryPage,wrapper);
        return R.success(categoryPage);
    }

    /**
     * 新增菜品分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> targetKind(@RequestBody Category category){
        category.setStatus(1L);
        categoryService.save(category);
        return R.success("保存成功！");
    }

    /**
     * 删除套餐
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteCategory(Long id){
        log.info("========>"+id.toString());
        categoryService.removeById(id);
        return R.success("移除成功！");
    }

    /**
     * 修改套餐
     * @param category
     * @return
     */
    @PutMapping
    public R<String> updateCategory(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功！");
    }

    /**
     * 数据回显
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> appear(String type){
        log.info(type);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type!=null,Category::getType,type);
        //执行查询
        List<Category> list = categoryService.list(queryWrapper);
        log.info(list.toString());
        return R.success(list);
    }

}

